package com.duanjw.bank.rate.handler;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.DateFormatUtil;
import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * 上海银行
 * <p>
 * 未经上海银行许可，任何组织或个人不得链接上海银行网站
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class ShangHaiBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://www.bosc.cn/apiQry/apiPCQry/doMCInterestRateQry");
        String responseBody = connect.headers(headers).ignoreContentType(true).execute().body();

        JSONObject responseJson = new JSONObject(responseBody).getJSONObject("data");

        String kaiShiZhiXingRiQi = getKaiShiZhiXingRiQi(null);
        String huoQiCunKuanLiLv = responseJson.getStr("CNYHQ_PIBS");

        String zhengCunZhengQu3YueLiLv = responseJson.getStr("3MCNYZCZQ_PIBS");
        String zhengCunZhengQu6YueLiLv = responseJson.getStr("6MCNYZCZQ_PIBS");
        String zhengCunZhengQu12YueLiLv = responseJson.getStr("1YCNYZCZQ_PIBS");
        String zhengCunZhengQu24YueLiLv = responseJson.getStr("2YCNYZCZQ_PIBS");
        String zhengCunZhengQu36YueLiLv = responseJson.getStr("3YCNYZCZQ_PIBS");
        String zhengCunZhengQu60YueLiLv = responseJson.getStr("5YCNYZCZQ_PIBS");

        String lingCunZhengQu12YueLiLv = responseJson.getStr("1YCNYLCZQ_PIBS");
        String lingCunZhengQu36YueLiLv = responseJson.getStr("3YCNYZCLQ_PIBS");
        String lingCunZhengQu60YueLiLv = responseJson.getStr("5YCNYCBQX_PIBS");


        String dingHuoLiangBian3YueLiLv = new BigDecimal(zhengCunZhengQu3YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        String dingHuoLiangBian6YueLiLv = new BigDecimal(zhengCunZhengQu6YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        String dingHuoLiangBian12YueLiLv = new BigDecimal(zhengCunZhengQu12YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();

        String tongZhiCunKuan1TianLiLv = responseJson.getStr("1DCNYTZCK_PIBS");
        String tongZhiCunKuan7TianLiLv = responseJson.getStr("7DCNYTZCK_PIBS");

        String xieDingCunKuanLiLv = responseJson.getStr("CNYXDCK_EIBS");

        return new BankRate(DateFormatUtil.format3(kaiShiZhiXingRiQi), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, getXieDingCunKuanLiLv(xieDingCunKuanLiLv));
    }

    @Override
    public String getBankName() {
        return "上海银行";
    }

    @SneakyThrows
    private String getKaiShiZhiXingRiQi(String input) {
        Connection connect = Jsoup.connect("https://www.bosc.cn/apiQry/apiPCQry/v2/getMCInterestRateDate");
        String body = connect.method(Connection.Method.POST).ignoreContentType(true).execute().body();
        return new JSONObject(body).getJSONObject("data").getStr("A");
    }

    private String getXieDingCunKuanLiLv(String xieDingCunKuanLiLv) {
        if (StrUtil.isBlank(xieDingCunKuanLiLv)) {
            return null;
        }
        return xieDingCunKuanLiLv;
    }

    public static void main(String[] args) {
        System.out.println(new BigDecimal("0.20000"));
    }
}
